package com.rp.configure.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rp.common.result.ResultDTO;
import com.rp.common.utils.NullIs;
import com.rp.common.utils.Selectway;
import com.rp.configure.entity.Operation;
import com.rp.configure.service.OperationService;
import com.rp.system.user.entity.SysUser;
import com.rp.system.user.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vicente
 * @since 2021-07-14
 */
@RestController
@RequestMapping("/opertion")
@Api(tags="操作控制")
public class OperationController {
    @Autowired
    private OperationService operationService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private Selectway selectway;

    @ApiOperation(value = "分页列表")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/list_page")
    public ResultDTO<IPage<Operation>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
        QueryWrapper<Operation> queryWrapper = new QueryWrapper<Operation>();
        queryWrapper.orderByDesc("create_time");
        return ResultDTO.success().data(operationService.page(new Page<Operation>(pageNum, pageSize), queryWrapper));
    }
    @ApiOperation(value = "列表")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/listByUser")
    public ResultDTO<List<Operation>> list( @RequestParam("userId")String userId) {
        QueryWrapper<Operation> queryWrapper = new QueryWrapper<Operation>();

        queryWrapper.orderByDesc("create_time").eq("user_id",userId);
        List<Operation> list = operationService.list();
        list.forEach(operation -> {
            switch (operation.getAgreement()){
                case 1:operation.setAgreementName("读线圈状态");
                case 2:operation.setAgreementName("读输入状态");
                case 3:operation.setAgreementName("读保持寄存器");
                case 4:operation.setAgreementName("读输入寄存器");
                case 5:operation.setAgreementName("强制写入单线");
                case 6:operation.setAgreementName("强制写入多线圈");
                case 16:operation.setAgreementName("预置多寄存器");
                case 17:operation.setAgreementName("报告从设备 ID");
                case 22:operation.setAgreementName("屏蔽写寄存器");
                case 23:operation.setAgreementName("读／写寄存器");
            }
        });
        return ResultDTO.success().data(list);
    }

    @ApiOperation(value = "列表根据用户id查询")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(path="/list_userId")
    public ResultDTO<IPage<Operation>> listByUserId(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                   @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                                                    @RequestParam("userId")String userId) {
        QueryWrapper<Operation> queryWrapper = new QueryWrapper<Operation>();

        if (!NullIs.isEmpty(userId)){

            queryWrapper.eq("user_id",userId).orderByDesc("create_time");
            IPage<Operation> page=operationService.listAddAgreeName(new Page<Operation>(pageNum, pageSize),userId,queryWrapper);
            return ResultDTO.success().data(page);
        }else {
            return ResultDTO.USER_NOT_LOGGED_IN().message("用户未登录，请登录后重新尝试操作");}

    }

    @PostMapping("/insert")
    @ApiOperation(value = "增加")
    @Transactional
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO insert(@RequestBody Operation opertion) {
        return ResultDTO.tf(operationService.save(opertion));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO update(@RequestBody Operation opertion) {
        return ResultDTO.tf(operationService.updateById(opertion));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO delete(@RequestBody @ApiParam(value = "id", required = true) String id) {
        return ResultDTO.tf(operationService.removeById(id));
    }

    @DeleteMapping("/deleteAll")
    @ApiOperation(value = "批量删除")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO deleteAll(@RequestBody @ApiParam(value = "id(英文逗号隔开)", required = true) String ids) {
        return ResultDTO.tf(operationService.removeByIds(Arrays.asList(ids.split(","))));
    }

}
